﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kovan.Model.Entity.UserCenter;
using Kovan.Model.Parameter.UserCenter;
using Kovan.Model.ViewModel.UserCenter;

namespace Kovan.DAL.UserCenter
{
    public class DActDept : IDAL<act_dept>
    {

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public IEnumerable<EDept> GetList(PDept param)
        {
            using (var db = new MysqlContext())
            {

                var query = from d in db.act_dept
                            join a in db.act_application on d.application_id.Value equals a.application_id.Value into left
                            from cleft in left.DefaultIfEmpty()
                            orderby d.dept_id descending
                            select new EDept
                            {
                                dept = d,
                                application = cleft
                            };

                if (!string.IsNullOrEmpty(param.Name))
                {
                    string Name = param.Name;
                    query = query.Where(o => o.dept.name.Contains(Name));
                }


                //总条数
                param.TotalRows = query.Count();

                if (param.PageSize > 0 && param.PageIndex > 0)
                {
                    query.Skip(param.PageSize * (param.PageIndex - 1)).Take(param.PageSize);
                }

                //return query.OrderByDescending(c => c.dept.dept_id.Value).ToList();
                return query.ToList();
            }
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="applicationid"></param>
        /// <returns></returns>
        public List<act_dept> GetList(int applicationid)
        {
            using (var db = new MysqlContext())
            {
                return db.act_dept.Where(c => c.application_id.Value == applicationid).ToList();
            }
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public  act_dept GetModel(long id)
        {
            using (var db = new MysqlContext())
            {
                return db.act_dept.FirstOrDefault(c => c.dept_id == id);
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public  int Add(act_dept model)
        {
            using (var db = new MysqlContext())
            {
                db.act_dept.Add(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <returns></returns>
        public  int Delete(long id)
        {
            using (var db = new MysqlContext())
            {
                var model = db.act_dept.FirstOrDefault(a => a.dept_id == id);
                db.act_dept.Remove(model);
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public  int BatchDelete(IEnumerable<string> ids)
        {
            using (var db = new MysqlContext())
            {
                foreach (var id in ids)
                {
                    var moduleid = long.Parse(id);
                    var model = db.act_dept.FirstOrDefault(a => a.dept_id == moduleid);
                    if (model != null)
                    {
                        db.act_dept.Remove(model);
                    }
                }
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="model">实体</param>
        /// <returns></returns>
        public  int Update(act_dept model)
        {
            using (var db = new MysqlContext())
            {
                db.Set<act_dept>().Attach(model);
                db.Entry(model).State = System.Data.EntityState.Modified;
                return db.SaveChanges();
            }
        }
    }
}
